local shanrang = fk.CreateSkill {
  name = "lingling__shanrang",
}

Fk:loadTranslationTable {
  ["lingling__shanrang"] = "禅让",
  [":lingling__shanrang"] = "每名角色限一次，当你濒死时，你可以将所有牌交给一名其他角色（至少一张），然后你摸两张牌并回复体力至1点。",

  ["#lingling__shanrang-choose"] = "禅让：你可以将所有牌交给一名其他角色，摸两张牌并回复体力至1"
}

shanrang:addEffect(fk.EnterDying, {
  anim_type = "defensive",
  can_trigger = function(self, event, target, player, data)
    local targets = table.filter(player.room:getOtherPlayers(player), function(p)
      return not table.contains(player:getTableMark(shanrang.name), p.id)
    end)
    event:setCostData(self, { targets = targets })
    return target == player and player:hasSkill(shanrang.name) and #targets > 0 and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local targets = event:getCostData(self).targets
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = shanrang.name,
      prompt = "#lingling__shanrang-choose",
      cancelable = true,
    })
    if #tos == 1 then
      event:setCostData(self, { target = tos[1] })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).target
    room:addTableMark(player, shanrang.name, to.id)
    if to.dead or player.dead or player:isNude() then return end
    room:moveCardTo(player:getCardIds("he"), Player.Hand, to, fk.ReasonGive, shanrang.name, nil, false, player)
    if player.dead then return end
    player:drawCards(2, shanrang.name)
    if player.dead and not player:isWounded() then return end
    room:recover {
      num = 1 - player.hp,
      skillName = shanrang.name,
      who = player,
      recoverBy = player
    }
  end,
})

return shanrang
